#!/bin/bash
#
# Copyright (C) 2024, 2025, Cyrus Li (e21920076@126.com). All rights reserved.
#

#
# A handy utility to set a group of aliases such as 'up', 'dw'
#
# XXX: Please include these lines in your bashrc
# export WSFILE='/tmp/.foo.updw'
# alias catupdw='egrep "foo.updw" ~/.bashrc | egrep -v "catupdw=" | egrep "(ud=|cq=|sq=)"'
# alias ud='bash ~/bin/updw'
# alias cq='[[ -f $WSFILE ]] && cat -n $WSFILE'
# alias sq='[[ -f $WSFILE ]] && source $wSFILE || echo "Oops, $WSFILE not found"'
# alias cqx="(export ISATTY=no; grep -E '#alias#.*dw' /tmp/.jq.updw | sed 's%\${__JQ_WSROOT}/%%g' | lsgen)"
#

FILE=$(readlink -f "${BASH_SOURCE[0]}")
CDIR=$(dirname "$FILE")
NAME=$(basename "$FILE")
TMPDIR=${TMPDIR:-"/tmp"}

WSROOT=${WSROOT:-"$HOME/Huanian/work"}
WSFILE=${WSFILE:-"$TMPDIR/.foo.updw"}
# XXX: Please specify env LX_PROJECT with your real project name, e.g. "lxtone"
LX_PROJECT=${LX_PROJECT:-"foo01"}

function print { echo -e "$*"; }

function _isatty
{
    typeset -l isatty=${ISATTY:-"auto"}
    [[ $isatty == "yes" ]] && return 0         # yes
    [[ $isatty == "no"  ]] && return 1         # no
    [[ -t 1 && -t 2 ]] && return 0 || return 1 # auto
}

function str2gray    { _isatty && print "\033[1;30m$@\033[m" || print "$@"; }
function str2red     { _isatty && print "\033[1;31m$@\033[m" || print "$@"; }
function str2green   { _isatty && print "\033[1;32m$@\033[m" || print "$@"; }
function str2yellow  { _isatty && print "\033[1;33m$@\033[m" || print "$@"; }
function str2blue    { _isatty && print "\033[1;34m$@\033[m" || print "$@"; }
function str2magenta { _isatty && print "\033[1;35m$@\033[m" || print "$@"; }
function str2cyan    { _isatty && print "\033[1;36m$@\033[m" || print "$@"; }
function str2white   { _isatty && print "\033[1;37m$@\033[m" || print "$@"; }

function get_common_envs
{
    echo "export __FOO_WSROOT=$WSROOT"
    echo "export __FOO_WSFILE=$WSFILE"
}

function get_one_alias_desc
{
    typeset this_alias=${1?"*** alias"}
    typeset this_path=${2?"*** path"}
    echo "#alias# ${this_alias}:${this_path}"
}

function get_one_alias
{
    typeset this_alias=${1?"*** alias"}
    typeset this_path=${2?"*** path"}
    typeset prefix=$(str2cyan '>>>')
    typeset joint=$(str2red '==>')
    echo $(printf \
          "alias ${this_alias}=' \
          __src_work_dir=\$(pwd -P); \
          cd ${this_path}; \
          __dst_work_dir=\$(pwd -P); \
          echo \"${prefix} \${__src_work_dir} ${joint} \${__dst_work_dir}\"; \
          unset __src_work_dir; \
          unset __dst_work_dir;' \
          ")
    get_one_alias_desc "$this_alias" "$this_path"
}

function get_l1_alias
{
    get_one_alias "up" "\${__FOO_WSROOT}"
    get_one_alias "dw" "\${__FOO_WSROOT}/${LX_PROJECT}"
    get_one_alias "oo" "~"
    get_one_alias "mm" "/tmp"
    get_one_alias "vt" "/var/tmp"
}

function get_l2_alias
{
    get_one_alias "dwdoc" "\${__FOO_WSROOT}/${LX_PROJECT}/doc"
}

echo "# !!! THIS FILE IS CREATED BY $NAME ON $(date +'%Y%m%d %H:%M:%S') !!!" > $WSFILE
get_common_envs >> $WSFILE
get_l1_alias    >> $WSFILE
get_l2_alias    >> $WSFILE
echo "# $WSFILE"
echo "DONE"
